package com.xiezc.util;

import com.alibaba.fastjson.support.retrofit.Retrofit2ConverterFactory;
import retrofit2.Retrofit;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by wb-xzc291800 on 2017/6/22.
 */
public class RetrofitUtil {


    public static ReadWriteLock readWriteLock = new ReentrantReadWriteLock();


    private static String baseUrl = "http://localhost:7900/";

    private static Retrofit.Builder retrofitBuilder = new Retrofit.Builder();

    private static Retrofit2ConverterFactory retrofit2ConverterFactory = new Retrofit2ConverterFactory();

    private static Retrofit getRetrofit() {
        try {
            readWriteLock.writeLock().lock();
            retrofitBuilder = retrofitBuilder.baseUrl(baseUrl);
        } finally {
            readWriteLock.writeLock().unlock();
        }
        return retrofitBuilder.addConverterFactory(retrofit2ConverterFactory).build();
    }

    public static <T> T create(Class<T> tClass) {
        return getRetrofit().create(tClass);
    }


}
